package com.tjl.synchronized_practice;

import javax.swing.plaf.synth.SynthComboBoxUI;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    int number;     //票数
    double price;     //价格
    ReentrantLock lock = new ReentrantLock();

    public Ticket() {
    }

    public Ticket(int number, double price) {
        this.number = number;
        this.price = price;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }


    //    public synchronized void  saleTicket(){
    public synchronized void saleTicket() {
        Thread thread = Thread.currentThread();
//        synchronized (this) {
        lock.lock();
        if (number > 0) {

            System.out.println(thread.getName() + "购买成功，剩余票数" + --number);
//            System.out.println("购买成功，剩余票数" + number);
        } else {
            System.out.println(thread.getName() + "没有票啦！剩余票数" + number);
        }
        lock.unlock();

//        }

    }
}
